package com.atguigu.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.atguigu.pojo.vo.PortalVo;
import com.atguigu.utils.JwtHelper;
import com.atguigu.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.pojo.Headline;
import com.atguigu.service.HeadlineService;
import com.atguigu.mapper.HeadlineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author dxikhw
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-06-19 21:22:23
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private HeadlineMapper headlineMapper;
    @Override
    public Result findNewsPage(PortalVo portalVo) {
        IPage<Map> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
        headlineMapper.selectMyPage(page, portalVo);
        HashMap<Object, Object> pageInfo = new HashMap<>();
        pageInfo.put("pageDate",page.getRecords());
        pageInfo.put("pageNum",page.getPages());
        pageInfo.put("pageSize",page.getSize());
        pageInfo.put("totalPage",page.getPages());
        pageInfo.put("totalSize",page.getTotal());

        HashMap<Object, Object> pageInfoMap = new HashMap<>();
        pageInfoMap.put("pageInfo",pageInfoMap);
        return Result.ok(pageInfoMap);
    }

    @Override
    public Result showHeadlineDetail(Integer hid) {
        Map data = headlineMapper.selectDetailMap(hid);
        Headline headline = new Headline();
        headline.setHid(hid);
        headline.setPageViews((Integer) data.get("pageViews")+1);
        headline.setVersion((Integer) data.get("version"));
        headlineMapper.updateById(headline);
        HashMap<Object, Object> headlinMap = new HashMap<>();
        headlinMap.put("headline",headlinMap);
        return Result.ok(headlinMap);
    }

    @Override
    public Result publish(Headline headline, String token) {
        int userId = jwtHelper.getUserId(token).intValue();
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        headlineMapper.insert(headline);
        return Result.ok(null);
    }

    @Override
    public Result updateData(Headline headline) {
        //System.out.println(headline);
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        headline.setVersion(version);
        headline.setUpdateTime(new Date());
        headlineMapper.updateById(headline);
        return Result.ok(null);
    }

}




